//============================================================
// Producnt name:		Wz
// Version: 			1.0
// Coded by:			Wz (13066628480@163.com)
// Auto generated at: 	2016/10/9 10:22:03
//============================================================

using System;
using System.Collections.Generic;
using System.Text;
using Cloud.Model;
using System.Data.SqlClient;
using System.Data;
namespace Cloud.DAL
{
    public static class AscriptionService
    {
        #region SqlCommandText
        private const string Save_CommandText = "insert into Ascription values(@name,@item1,@item2)";
        private const string Update_CommandText = "Update Ascription set name = @name,item1 = @item1,item2 = @item2 where id = @id";
        private const string Delete_CommandText = "delete from Ascription where id = @id";
        private const string FindAll_CommandText = "select * from Ascription";
        private const string FindById_CommandText = "select * from Ascription where id=@id";
        private const string FindByName_CommandText = "select * from Ascription where Name=@Name";
        #endregion

        #region ??

        #endregion

        #region ??

        #endregion

        #region Save
        public static int Save(Ascription ascription)
        {
            SqlParameter[] pars = new SqlParameter[]{
				new SqlParameter("@name",SqlDbType.VarChar),
				new SqlParameter("@item1",SqlDbType.Int),
				new SqlParameter("@item2",SqlDbType.VarChar)
				};
            pars[0].Value = ascription.Name;
            pars[1].Value = ascription.Item1;
            pars[2].Value = ascription.Item2;

            return DBHelper.ExecuteNonQuery(Save_CommandText, CommandType.Text, pars);
        }
        #endregion

        #region Update
        public static int Update(Ascription ascription)
        {
            SqlParameter[] pars = new SqlParameter[]{
				new SqlParameter("@id",SqlDbType.Int),
				new SqlParameter("@name",SqlDbType.VarChar),
				new SqlParameter("@item1",SqlDbType.Int),
				new SqlParameter("@item2",SqlDbType.VarChar)
				};
            pars[0].Value = ascription.Id;
            pars[1].Value = ascription.Name;
            pars[2].Value = ascription.Item1;
            pars[3].Value = ascription.Item2;

            return DBHelper.ExecuteNonQuery(Update_CommandText, CommandType.Text, pars);
        }
        #endregion

        #region Delete
        public static int Delete(Ascription ascription)
        {
            SqlParameter[] pars = new SqlParameter[]{
				new SqlParameter("@id",SqlDbType.Int)
				};
            pars[0].Value = ascription.Id;

            return DBHelper.ExecuteNonQuery(Delete_CommandText, CommandType.Text, pars);
        }
        #endregion

        #region FindAll
        public static IList<Ascription> FindAll()
        {
            return CommonQuery(FindAll_CommandText, CommandType.Text, null);
        }
        #endregion

        #region FindById
        public static Ascription FindById(Int32 id)
        {
            SqlParameter[] pars = new SqlParameter[]{
				new SqlParameter("@id",SqlDbType.Int)
			};
            pars[0].Value = id;
            IList<Ascription> list = CommonQuery(FindById_CommandText, CommandType.Text, pars);
            return list.Count > 0 ? list[0] : null;
        }
        #endregion

        #region FindByName
        public static Ascription FindByName(String Name)
        {
            SqlParameter[] pars = new SqlParameter[]{
				new SqlParameter("@Name",SqlDbType.VarChar)
			};
            pars[0].Value = Name;
            IList<Ascription> list = CommonQuery(FindByName_CommandText, CommandType.Text, pars);
            return list.Count > 0 ? list[0] : null;
        }
        #endregion

        #region ??

        #endregion

        #region CommonQuery
        private static IList<Ascription> CommonQuery(string commandText, CommandType cmdType, SqlParameter[] pars)
        {
            IList<Ascription> list = new List<Ascription>();
            Ascription ascription = null;

            using (SqlDataReader reader = DBHelper.ExecuteQuery(commandText, cmdType, pars))
            {
                while (reader.Read())
                {
                    ascription = new Ascription();
                    if (reader["id"] == null)
                    {
                        ascription.Id = 0;
                    }
                    else
                    {
                        ascription.Id = Convert.ToInt32(reader["id"]);
                    }
                    if (reader["name"] == null)
                    {
                        ascription.Name = "";
                    }
                    else
                    {
                        ascription.Name = Convert.ToString(reader["name"]);
                    }

                    if (reader["item1"] == null)
                    {
                        ascription.Item1 = 0;
                    }
                    else
                    {
                        ascription.Item1 = Convert.ToInt32(reader["item1"]);
                    }

                    if (reader["item2"] == null)
                    {
                        ascription.Item2 = "";
                    }
                    else
                    {
                        ascription.Item2 = Convert.ToString(reader["item2"]);
                    }


                    list.Add(ascription);
                }
            }
            return list;
        }
        #endregion

        #region ??

        #endregion

        #region ??

        #endregion
    }
}



